<template>
  <el-form :model="ActivityRegistryForm" status-icon :rules="rules" ref="ActivityRegistryForm"
           label-width="auto">
    <h2 class="region_title">Activity</h2>
    <el-form-item
      v-for="(name, index) in ActivityRegistryForm.name"
      :label="'activity名称' + index"
      :key="name.key"
      :prop="'name.' + index + '.value'"
      :rules="{
      required: true, message: 'activityName不能为空', trigger: 'blur'
    }"
    >
      <el-input v-model="name.value" placeholder="activity名称，语言map 例：en:name"></el-input>
      <el-button @click.prevent="removeDomain(name)">删除</el-button>
    </el-form-item>
    <el-form-item>
      <el-button @click="addDomain">新增名称</el-button>
    </el-form-item>
    <el-form-item prop="description" label="description">
      <el-input type="text" v-model="ActivityRegistryForm.description"
                placeholder="description"></el-input>
    </el-form-item>
    <el-form-item prop="type" label="activity类型">
      <el-input type="text" v-model="ActivityRegistryForm.type"
                placeholder="activity类型"></el-input>
    </el-form-item>
  </el-form>
</template>
<script>
  export default {

    mounted: function () {
      this.getActivity();
    },
    data() {
      return {
        ActivityRegistryForm: {
          name: [
            {value: ''}
          ],
          description:'',
          type:'',
        },
        type:'',
        description:'',
        rules: {
          type:[
            {}
          ]
        }
      };
    },
    methods: {
      getActivity() {
        if (this.$parent.data !== null) {
          this.$axios.get("/activity/findOne", {
            params: {
              activityId: this.$parent.data,
            }
          }).then(successResponse => {
            this.ActivityRegistryForm = successResponse.data
          }).catch(failResponse => {
          })
        }
      },
      submitForm() {
        this.$refs['ActivityRegistryForm'].validate((valid) => {
          if (valid) {
            let url = ''
            let id=''
            if (this.$parent.data !== null) {
              //更新
              url = '/activity/update'
              id=this.$parent.data
            } else {
              //添加
              url = '/activity'
            }
            console.log(JSON.stringify(this.ActivityRegistryForm.displays))
            this.$axios
              .post(url, {
                activityId:id,
                name: this.ActivityRegistryForm.name,
                description: this.ActivityRegistryForm.description,
                type:this.ActivityRegistryForm.type
              })
              .then(success => {
                this.$message({
                  type: 'info',
                  message: "提交成功！"
                });
              }).catch(fail => {
              this.$message.error("提交失败")
            })
          } else {
            this.$message.error("编辑信息有误")
            return false;
          }
        });
      },
      resetForm() {
        this.$refs['ActivityRegistryForm'].resetFields();
      },
      removeDomain(item) {
        var index = this.ActivityRegistryForm.name.indexOf(item)
        if (index !== -1) {
          this.ActivityRegistryForm.name.splice(index, 1)
        }
      },
      addDomain() {
        this.ActivityRegistryForm.name.push({
          value: '',
          key: Date.now()
        });
      }
    }
  }
</script>

<style scoped>
  .region_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: darkslateblue;
  }

</style>
